package com.supermap.dialogs;

import com.supermap.data.Enum;
import com.supermap.data.FieldInfo;
import com.supermap.data.FieldType;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import java.awt.*;

public class FieldInfoAddedDialog extends CJDialog {
    private JTextField nameTextField;
    private JTextField captionTextField;
    private JComboBox<FieldType> fieldTypeComboBox;
    private JTextField maxLengthTextField;
    private JTextField defaultValueTextField;
    private JCheckBox isRequiredCheckBox;

    private FieldInfo fieldInfo;

    public FieldInfoAddedDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal, 300, 240, null);
    }

    public FieldInfo getFieldInfo() {
        return fieldInfo;
    }

    @Override
    protected void initializeArgs(Object[] args) {

    }

    @Override
    protected void initializeCore() {
        super.confirmBtn.setText("添加");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(6, 1));
        add(mainPanel, BorderLayout.CENTER);

        JLabel nameLabel = new JLabel("字段名: ");
        nameTextField = new JTextField();
        JPanel namePanel = new JPanel();
        namePanel.setLayout(new BorderLayout());
        namePanel.add(nameLabel, BorderLayout.WEST);
        namePanel.add(nameTextField, BorderLayout.CENTER);
        mainPanel.add(namePanel);

        JLabel captionLabel = new JLabel("字段别名: ");
        captionTextField = new JTextField();
        JPanel captionPanel = new JPanel();
        captionPanel.setLayout(new BorderLayout());
        captionPanel.add(captionLabel, BorderLayout.WEST);
        captionPanel.add(captionTextField, BorderLayout.CENTER);
        mainPanel.add(captionPanel);

        JLabel fieldTypeLabel = new JLabel("字段类型: ");
        fieldTypeComboBox = new JComboBox<>();
        Enum[] fieldTypes = FieldType.getEnums(FieldType.class);
        for (Enum fieldType : fieldTypes) {
            fieldTypeComboBox.addItem((FieldType) fieldType);
        }
        JPanel fieldTypePanel = new JPanel();
        fieldTypePanel.setLayout(new BorderLayout());
        fieldTypePanel.add(fieldTypeLabel, BorderLayout.WEST);
        fieldTypePanel.add(fieldTypeComboBox, BorderLayout.CENTER);
        mainPanel.add(fieldTypePanel);

        JLabel maxLengthLabel = new JLabel("最大长度: ");
        maxLengthTextField = new JTextField();
        JPanel maxLengthPanel = new JPanel();
        maxLengthPanel.setLayout(new BorderLayout());
        maxLengthPanel.add(maxLengthLabel, BorderLayout.WEST);
        maxLengthPanel.add(maxLengthTextField, BorderLayout.CENTER);
        mainPanel.add(maxLengthPanel);

        JLabel defaultValueLabel = new JLabel("缺省值: ");
        defaultValueTextField = new JTextField();
        JPanel defaultValuePanel = new JPanel();
        defaultValuePanel.setLayout(new BorderLayout());
        defaultValuePanel.add(defaultValueLabel, BorderLayout.WEST);
        defaultValuePanel.add(defaultValueTextField, BorderLayout.CENTER);
        mainPanel.add(defaultValuePanel);

        JLabel isRequiredLabel = new JLabel("是否必填: ");
        isRequiredCheckBox = new JCheckBox();
        JPanel isRequiredPanel = new JPanel();
        isRequiredPanel.setLayout(new BorderLayout());
        isRequiredPanel.add(isRequiredLabel, BorderLayout.WEST);
        isRequiredPanel.add(isRequiredCheckBox, BorderLayout.CENTER);
        mainPanel.add(isRequiredPanel);
    }

    @Override
    protected void doConfirmCore() {
        this.setVisible(false);
        fieldInfo = new FieldInfo();
        fieldInfo.setName(nameTextField.getText());
        fieldInfo.setCaption(captionTextField.getText());
        fieldInfo.setType((FieldType) fieldTypeComboBox.getSelectedItem());
        fieldInfo.setMaxLength(Integer.parseInt(maxLengthTextField.getText()));
        fieldInfo.setDefaultValue(defaultValueTextField.getText());
        fieldInfo.setRequired(isRequiredCheckBox.isSelected());
    }
}
